package pages;

import org.openqa.selenium.By;
import org.testng.Assert;
import ui_test.TestData;
import utils.Log4Test;

/**
 * Created with IntelliJ IDEA.
 * User: u
 * Date: 7/22/14
 * Time: 4:36 PM
 * To change this template use File | Settings | File Templates.
 */
public class SearchedProductPage extends GeneralPage {
    protected By buyButton = By.xpath("//div[@class='pp-price-status-block']//button[@type='submit']");
    protected By popUpWindowClosing = By.xpath("//div[@class='cart-popup']//a[@class='popup-close']");
    protected By justAddedProductTitle = By.xpath("//div[@class='cart-i-title cart-added-title']/a");
    protected By justAddedProductRemovingCross = By.xpath("//div[@class='clearfix cart-i cart-added active']//a[@name='delete']");
    protected By basketEmptyTitle = By.xpath("//div[@class='cart-popup']//h2");
    protected By signOutLink = By.xpath(".//*[@id='user_menu']//a[@name='signout']");
    protected By signInLink = By.xpath(".//*[@id='user_menu']//a");

    public void productBuying() {
        Log4Test.info("Buying product");
        webDriver.findElement(buyButton).click();
    }

    public void productAppearanceInBasketVerifying() {
        Log4Test.info("product appearance in recycle verifying");
        Assert.assertEquals(webDriver.findElement(justAddedProductTitle).getText(), TestData.firstProductName);
    }

    public void justAddedProductRemoving() {
        Log4Test.info("just added product removing from basket");
        webDriver.findElement(justAddedProductRemovingCross).click();

    }

    public void verifyingBasketIsEmpty() {
        Log4Test.info("verifying basket is empty");
        try {
            Thread.sleep(3 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }

        Assert.assertEquals(webDriver.findElement(basketEmptyTitle).getText(), "Корзина пуста");
    }

    public void basketClosing() {
        Log4Test.info("Close basket");
        webDriver.findElement(popUpWindowClosing).click();
    }

    public void signOut() {
        Log4Test.info("Signing out");
        webDriver.findElement(signOutLink).click();
    }

    public void signOutVerify() {
        Log4Test.info("presence of signing in link appearing checking");
        try {
            Thread.sleep(2 * 1000);
        } catch (InterruptedException e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        Assert.assertEquals(webDriver.findElement(signInLink).getText(), "Вход в интернет-магазин");
    }

    public void urlVerify() {
        Log4Test.info("home url verify");
        Assert.assertTrue(webDriver.getCurrentUrl().contains(TestData.HomeURL));
    }
}                                                               
